// import 'babel-polyfill';
import MainApi from './routes/api';
import cookieParser from 'cookie-parser';
import path from 'path';
import express from 'express';
import cors from 'cors';
import { IMGAE_BASE, BASE_API, HOST } from './server.config';
import { IMGS_PATH } from './utils/upload';
import { pass_one_day } from './corn/index';
import { START_PORT } from './server.config';

const app = express();
const makepath = (e) => path.join(__dirname, e);

// const history = require('connect-history-api-fallback')
// app.use('/',history())

app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

//解析数据 - 文件大小限制
const bodyParser = require('body-parser'); //
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(bodyParser.json({ limit: '50mb' }));

//请求头-跨域
app.use(cors({ origin: '*' }));

// 托管前端文件
app.use(express.static(makepath('./dist')));
// 图片路径
console.log(IMGS_PATH);
app.use(IMGAE_BASE, express.static(IMGS_PATH)); // 图片

// 挂载API
app.use(BASE_API, MainApi);

global.pakchoi = 1;

// 启动服务.
const http = require('http');
const server = http.createServer(app);
server.listen(START_PORT, () => {
	console.log(global.process.version, `运行成功: ${HOST}:${START_PORT}`);
	pass_one_day(); //每天执行的任务
});
